<!--
 * @Description: 操作会议
 * @Author: wind-lc
 * @version: 1.0
 * @Date: 2023-02-06 14:37:01
 * @LastEditTime: 2023-04-11 11:42:40
 * @FilePath: \cscec-pms-admin\src\views\project\projectPlanningMeeting\operation.vue
-->
<template>
  <base-breadcrumb
    :breadcrumb="!isAudit"
    :title="title"
  >
    <MeetingOperation
      ref="meetingOperation"
      :form="form"
      :meeting-type="'16'"
      :form-option="formOption"
      :rules="rules"
      :page-type="pageType"
      :use-attachment="useAttachment"
      :use-submit="useSubmit"
      :title="'基本信息'"
      @getDate="getDate"
    >
      <template slot="signNum">
        <div class="outcome-meeting-operation-btn">
          <BaseInput
            v-model="form.signNum"
            disabled
          />
          <a-button
            type="primary"
            @click="signInVisible = true"
          >
            查看签到
          </a-button>
          <ConferenceScreen
            v-if="pageType !== 'view'"
            :id="id"
            :meeting-screen="training.meetingScreen"
          />
        </div>
      </template>
      <template
        slot="attachment"
        slot-scope="data"
      >
        <a-card>
          <title-name title="策划资料" />
          <UploadTableNew
            ref="uploadTable"
            :disabled="pageType !== 'upload'"
            :mode="fileOption.mode"
            :init-file-list="fileOption.fileList"
            :need-page="fileOption.needPage"
            :business-code="fileOption.businessCode"
            :use-delete-creator="true"
            :use-create-time="true"
            @uploaded="handleUploadChange('uploaded')"
            @deleted="handleUploadChange('deleted')"
          />
        </a-card>
        <a-card v-if="pageType !== 'detail'">
          <title-name title="评审意见" />
          <div class="mb12">
            <a-button
              :loading="reviewLoading"
              type="primary"
              :disabled="!Array.isArray(data.data.commentsList) || data.data.commentsList.length === 0"
              @click="handleExport('review')"
            >导出评审意见</a-button>
            <a-button
              type="primary"
              class="ml12"
              :loading="downLoading"
              :disabled="!Array.isArray(data.data.commentsList) || !data.data.commentsList.some(el=> el.fileInfoList.length>0)"
              @click="handleExport('attachment',data.data.commentsList)"
            >一键导出附件</a-button>
            <a-button
              v-if="pageType === 'review' && !data.data.commentsList.some((el)=>el.creatorId === $store.state.user.userInfo.id)"
              type="primary"
              class="ml12"
              @click="handleOperation({}, 'add')"
            >添加评审意见</a-button>
          </div>
          <a-table
            :columns="columns"
            :data-source="data.data.commentsList"
            :loading="loading"
          >
            <template
              slot="action"
              slot-scope="text, record"
            >
              <div class="pub-table-list-action">
                <span @click="handleOperation(record, 'detail')">查看</span>
                <!-- <template v-if="$store.state.user.userInfo.id === record.creatorId && pageType === 'review'">
                  <span @click="handleOperation(record, 'update')">编辑</span>
                  <span @click="handleOperation(record, 'delete')">删除</span>
                </template> -->
              </div>
            </template>
          </a-table>
        </a-card>
      </template>
    </MeetingOperation>
    <!-- 查看签到模态框 -->
    <SeeSign
      :id="id"
      :visible="signInVisible"
      :is-new="true"
      @cancel="signInVisible = false"
      @confirm="signInVisible = false"
    />
    <!-- 查看签到模态框 -->
    <!-- 添加评审意见模态框 -->
    <ReviewerComment
      :visible.sync="reviewVisible"
      :reviewData="reviewData"
      @refresh="handleReviewRefresh"
      :meetingId="id"
    />
    <!-- 添加评审意见模态框 -->
  </base-breadcrumb>
</template>
<script>
import MeetingOperation from '@/components/Meeting/MeetingOperation'
import SeeSign from '@/components/seeSign'
import ConferenceScreen from '@/components/ConferenceScreen'
import training from '@/api/design/training'
import { formOption1, columns } from './model.js'
import ReviewerComment from './components/ReviewerComment'
import BasePage from '@/views/basePage'
import { technicalStandardDetailedListExport } from '@/api/technology.js'
export default {
  name: 'projectPlanningMeetingOperation' + Date.now(),
  extends: new BasePage(),
  props: {
    isAudit: {
      // 是否作为审批详情
      type: Boolean,
      default: false
    }
  },
  components: {
    MeetingOperation,
    SeeSign,
    ConferenceScreen,
    ReviewerComment
  },
  data() {
    return {
      // 页面名称
      pageName: {
        detail: '会议详情',
        enter: '进入会议',
        view: '查看会议',
        upload: '上传策划资料',
        review: '评审意见'
      },
      // 接口
      training,
      //表单
      form: {
        projectId: '',
        meetingName: this.$route.query.meetingName,
        meetingLevel: '',
        speaker: '',
        meetingPlace: '',
        deepenResultsList: '',
        meetingTime: '',
        createUser: '',
        createTime: '',
        signNum: '',
        meetingSummary: ''
      },
      // 签到id
      id: this.$route.query.id,
      // 签到模态框显示隐藏
      signInVisible: false,
      // 文件配置
      fileOption: {
        // 处理操作的模式：'add'模式下，删除操作会调用文件删除接口，'edit'模式只会在表格中移除删除数据，不会调用接口
        mode: 'add',
        // 上传文件的业务编码
        businessCode: 'PM47',
        // 是否需要分页
        needPage: false,
        // 初始化文件列表
        fileList: []
      },
      // 评审意见加载
      loading: false,
      // 评审意见表头
      columns,
      // 评审意见显示隐藏
      reviewVisible: false,
      // 评审意见数据
      reviewData: {
        type: 'add',
        data: {}
      },
      // 策划资料上传中
      uploadLoading: false,
      // 评审意见导出中
      reviewLoading: false,
      // 是否创建人
      isCreateUser: false
    }
  },
  computed: {
    // 页面类型
    pageType() {
      return this.isAudit ? 'view' : this.$route.query.pageType
    },
    // 标题
    title() {
      return this.pageName[this.pageType]
    },
    // 表单配置
    formOption() {
      // 待开始没有会议纪要
      return this.pageType == 'detail'
        ? formOption1.slice(0, -1)
        : formOption1.map((el, index) => {
            // 会议纪要在进行中才能编辑
            if (index === formOption1.length - 1) {
              el.disabled = this.pageType !== 'enter' || !this.isCreateUser
            }
            return el
          })
    },
    // 校验
    rules() {
      const obj = {}
      this.formOption.map((item) => {
        if (item.rules) {
          obj[item.keyName] = item.rules
        }
      })
      return obj
    }
  },
  watch:{
    form:{
      handler(val){
        this.isCreateUser = val?.createUserId === this.$store.state.user.userInfo.id
      },
      deep: true
    }
  },
  methods: {
    /**
     * @description: 自定义附件
     * @param {object} vm this对象
     * @param {object} data 详情接口返回数据
     * @return {void}
     */
    useAttachment(vm, data) {
      this.fileOption.fileList = data.uploadList
    },
    /**
     * @description: 自定义提交
     * @param {object} vm this对象
     * @return {void}
     */
    useSubmit(vm) {
      this.$confirm({
        title: '提交确认',
        content: '请确认是否要提交会议纪要，提交后会议结束',
        onOk() {
          vm.submitLoading = true
          training
          .updateByPrimaryKeySelective({
            id: vm.$route.query.id,
            meetingType: vm.meetingType,
            meetingSummary: vm.form.meetingSummary,
            signNum: vm.form.signNum,
            projectId: vm.form.projectId
          })
          .then(({ code, message }) => {
            if (code === 200) {
              vm.$message.success('提交成功')
              vm.handleClose()
            } else {
              vm.$message.warning(message)
            }
          })
          .finally(() => {
            this.submitLoading = false
          })
        }
      })
      
    },
    /**
     * @description: 导出
     * @param {string} type 操作类型
     * @param {array} list 评审意见列表
     * @return {void}
     */
    async handleExport(type, list = []) {
      if (type === 'review') {
        // 导出评审意见(复制于领导带班生产记录)
        try {
          const datas = list.map((el) => el.id)
          if (datas.length > 40) {
            this.$message.warning('最多导出40条数据！')
            return
          }
          this.reviewLoading = true
          let origin = window.location.origin
          let url = '',type = 0
          if(origin==='https://pm.cscecxn.com'){
            type = 1
            url= 'https://www.cscecxn.com:8012/'
          }else{
            type = 2
            url = 'http://192.168.144.24:8012/'
          }
          let data = await training.getMeetingReviewCommentsExportHtml({
            id: this.id
          })
          let psotUrl = `${url}downSafeTakeShiftByPdfGet`
          if (data.code === 200) {
            let req = data.data
            req.urlStr = window.location.origin
            delete req.accessToken
            let linkUrl = `${psotUrl}?fileId=${req.fileId}&fullfilename=${req.fullfilename}&type=${type}&filePrefixName=评审意见_`
            const link = document.createElement('a')
            link.style.display = 'none'
            link.href = linkUrl
            link.setAttribute('target', '_self')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link)
          }
        } catch (error) {
          console.log(error)
        } finally {
          this.reviewLoading = false
        }
      } else {
        console.log(list)
        // 导出所有附件
        this.downZip(list)
      }
    },
    /**
     * @description: 刷新评审意见
     * @return {void}
     */
    handleReviewRefresh() {
      this.$refs.meetingOperation.getConferenceDetail()
    },
    /**
     * @description: 操作
     * @param {object} record 行数据
     * @param {string} type 操作类型
     * @return {void}
     */
    handleOperation(record, type) {
      switch (type) {
        case 'add':
          // 新增
          this.reviewVisible = true
          this.reviewData = {
            type,
            data: {}
          }
          break
        case 'detail':
          // 查看
          this.reviewVisible = true
          this.reviewData = {
            type,
            data: record
          }
          break
        case 'update':
          // 编辑
          this.reviewVisible = true
          this.reviewData = {
            type,
            data: record
          }
          break
        case 'delete':
          // 删除
          this.handleDelete(record)
          break
      }
    },
    /**
     * @description: 上传策划资料变动(上传/删除)
     * @return {void}
     */
    handleUploadChange(type) {
      training
        .uploadPlanningMaterials({
          id: this.id,
          uploadList: this.fileOption.fileList.map((el) => {
            const item = this.$clone(el)
            delete item.id
            return item
          })
        })
        .then((res) => {
          if (res.code === this.SUCCESS_CODE) {
            if (type === 'uploaded') {
              this.$message.success('上传成功')
            } else {
              this.$message.success('删除成功')
            }
            this.handleReviewRefresh()
          }
        })
    },
    /**
     * @description:会议详情加载完成后修改数据
     * @return {void}
     */    
    getDate(){
      this.form.createTime = this.form.createTime.split(' ')[0]
    },
    /**
     * @description: 删除评审意见
     * @param {object} record 行数据
     * @return {void}
     */    
    handleDelete(record){
      const vm = this
      this.$confirm({
        title: '温馨提示',
        content: `确认删除？`,
        onOk() {
          return new Promise((resolve, reject) => {
            training.deleteReviewComments(record.id).then(res=>{
              if (res.code === vm.SUCCESS_CODE) {
                vm.$message.success('删除成功')
                vm.handleReviewRefresh()
                resolve()
              }else{
                reject()
              }
            }).catch(()=>{
              reject()
            })
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.outcome-meeting-operation-btn {
  display: flex;
  align-items: center;
  .BaseInput-container {
    flex: 1;
  }
  .ant-btn {
    height: 28px;
    margin-left: 10px;
  }
}
</style>
